﻿/**
 * jQuery EasyUI 1.4
 * 
 * Copyright (c) 2009-2014 www.jeasyui.com. All rights reserved.
 * 
 * Licensed under the GPL license: http://www.gnu.org/licenses/gpl.txt To use it
 * on other terms please contact us at info@jeasyui.com
 * 
 */
(function($) {
	
	$.fn._remove = function() {
		return this.each(function() {
			$(this).remove();
			try {
				this.outerHTML = "";
			} catch (err) {
			}
		});
	};
	function removeNode(target) {
		target._remove();
	};
	
	/**上移*/
	function upQuestion(target){
		var qtiBoxWidget=$(target).parents(".qtiBox-widget-box");
		var prevQtiBox=qtiBoxWidget.prev();
		if(prevQtiBox.length>0){ 
			var qtiBox = $.data(target, "qtiBox");
			var opts = qtiBox.options;
			qtiBoxWidget.insertBefore(prevQtiBox); 
			$(opts.testBox).testBox("exchangeUp",opts.question);
		} 
	}
	/**下移*/
	function downQuestion(target){
		var qtiBoxWidget=$(target).parents(".qtiBox-widget-box");
		var nextQtiBox=qtiBoxWidget.next();
		if(nextQtiBox.length>0){
			var qtiBox = $.data(target, "qtiBox");
			var opts = qtiBox.options;
			qtiBoxWidget.insertAfter(nextQtiBox); 
			$(opts.testBox).testBox("exchangeDown",opts.question);
		}
	}
	
	function resizeBox(target, options) {
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		var pqtiBox = qtiBox.qtiBox;
		var qtiBoxHeader = pqtiBox.children("div.qtiBox-header");
		var qtiBoxBody = pqtiBox.children("div.qtiBox-body");
		if (options) {
			$.extend(opts, {
				width : options.width,
				height : options.height,
				minWidth : options.minWidth,
				maxWidth : options.maxWidth,
				minHeight : options.minHeight,
				maxHeight : options.maxHeight
			});
		}
		pqtiBox._size(opts);
		qtiBoxHeader.add(qtiBoxBody)._outerWidth(pqtiBox.width());
		if (!isNaN(parseInt(opts.height))) {
			qtiBoxBody._outerHeight(pqtiBox.height() - qtiBoxHeader._outerHeight());
		} else {
			qtiBoxBody.css("height", "");
			var minHeight = $.parser.parseValue("minHeight", opts.minHeight, pqtiBox.parent());
			var maxHeight = $.parser.parseValue("maxHeight", opts.maxHeight, pqtiBox.parent());
			var outerHeight = qtiBoxHeader._outerHeight() + pqtiBox._outerHeight() - pqtiBox.height();
			qtiBoxBody._size("minHeight", minHeight ? (minHeight - outerHeight) : "");
			qtiBoxBody._size("maxHeight", maxHeight ? (maxHeight - outerHeight) : "");
		}
		pqtiBox.css({
			height : "",
			minHeight : "",
			maxHeight : ""
		});
		opts.onResize.apply(target, [ opts.width, opts.height ]);
		$(target).qtiBox("doLayout");
	};
	function wrapQtiBox(target) {
		$(target).addClass("qtiBox-body")._size("clear");
		var qtiBox = $("<div class=\"qtiBox-widget-box\" tabindex=\"-1\"></div>").insertBefore(target);
		
		qtiBox[0].appendChild(target);
		qtiBox.bind("_resize", function(e, _bl) {
			if ($(this).hasClass("easyui-fluid") || _bl) {
				resizeBox(target);
			}
			return false;
		});
		return qtiBox;
	};
	function setBorder(target) {
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		var pp = qtiBox.qtiBox;
		pp.css(opts.style);
		pp.addClass(opts.cls);
		setHeader(target);
		var qtiBoxHeader = $(target).qtiBox("header");
		var qtiBoxBody = $(target).qtiBox("body");
		if (opts.border) {
			qtiBoxHeader.removeClass("qtiBox-header-noborder");
			qtiBoxBody.removeClass("qtiBox-body-noborder");
		} else {
			qtiBoxHeader.addClass("qtiBox-header-noborder");
			qtiBoxBody.addClass("qtiBox-body-noborder");
		}
		qtiBoxHeader.addClass(opts.headerCls);
		qtiBoxBody.addClass(opts.bodyCls);
		$(target).attr("id", opts.id || "");
		
		if (opts.contentCall) {
			$(target).qtiBox("clear");
			qtiBoxBody.append(opts.contentCall.call(target));
		}
	};
	function fnPoint(target,div,index){
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		//分数赋值
		var iScore = index || opts.question.difficulty || 0;
		var i = 0;
		div.find("li").each(function(){
			this.className = i++ < iScore ? "on" : "";
		});
	}

	function setHeader(target) {
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		var pp = qtiBox.qtiBox;
		removeNode(pp.children("div.qtiBox-header"));
		if (opts.title && !opts.noheader && opts.status!="edit-sleep") {
			var header = $("<div class=\"qtiBox-header\"></div>").prependTo(pp);
			$("<span class=\"qtiBox-header-title\"></span>").html(opts.title).appendTo(header);
			var tool = $("<div class=\"qtiBox-header-tool\"></div>").appendTo(header);

			var div=$("<div class=\"ch_starOne\"></div>").appendTo(tool);
			$("<span>难度</span>").appendTo(div);
			var ul=$("<ul></ul>").appendTo(div);
			if(!opts.question.difficulty){
				opts.question.difficulty=0;
			}
			for(var i=1;i<=5;i++){
				var sName= i<=opts.question.difficulty?"on":"";
				$("<li index=\""+i+"\" class=\""+sName+"\"><a href=\"javascript:;\">"+i+"</a></li>").appendTo(ul).mouseover(function(){
					fnPoint(target,div,parseInt($(this).attr("index")));
				}).mouseout(function(){
					fnPoint(target,div);
				}).click(function(){
					var qtiBox = $.data(target, "qtiBox");
					var opts = qtiBox.options;
					opts.question.difficulty=parseInt($(this).attr("index"));
				});
			}
			
			$("<span class=\"separator\"> | </span>").appendTo(tool);
			if(opts.question && opts.question.showScore){
				var scoreLabel=$("<label>分</label>").appendTo(tool);
				opts.scoreInput=$("<input>");
				
				scoreLabel.append(opts.scoreInput);
				var score=0;
				if(opts.question && opts.question.score){
					score=opts.question.score;
				}
				opts.scoreInput.numberspinner({
					width:50,value:score,min:0,max:100,
					onChange:function(newValue, oldValue){
						opts.question.score=newValue;
						opts.question.oldValue=oldValue;
						var testBoxOpts=$(opts.testBox).testBox("options");
						if(testBoxOpts){
							testBoxOpts.onCountFullMarks.call(target,opts.question);
						}
					}
				});
				$("<span class=\"separator\"> | </span>").appendTo(tool);
			}
			
		//	$("<a class=\"selected\" href=\"javascript:void(0)\">上移</a>").appendTo(tool).bind("mousedown", function() {
		//		 upQuestion(target);
		//	});
		//	$("<a class=\"selected\" href=\"javascript:void(0)\">下移</a>").appendTo(tool).bind("mousedown", function() {
		//		 downQuestion(target);
		//	});
		//	$("<span class=\"separator\"> | </span>").appendTo(tool);
			 
			opts.questionBut=$("<a class=\"selected\" href=\"javascript:void(0)\">\u8BD5\u9898</a>");
			opts.questionBut.appendTo(tool).bind("mousedown", function() {
				opts.questionButEvent(target);
			});
			
	        $("<span class=\"separator\"> | </span>").appendTo(tool);
	        opts.responseBut=$("<a class=\"link\" href=\"javascript:void(0)\">\u7B54\u6848</a>");
	        opts.responseBut.appendTo(tool).bind("mousedown", function() {
	        	opts.responseButEvent(target);
			});
	        var operTool=$("<div class=\"qtiBox-operTool\"></div>").appendTo(tool);
	        $("<a class=\"qtiBox-tool-up\" href=\"javascript:void(0)\"></a>").appendTo(operTool).bind("mousedown", function() {
				 upQuestion(target);
			});
	        $("<a class=\"qtiBox-tool-down\" href=\"javascript:void(0)\"></a>").appendTo(operTool).bind("mousedown", function() {
				 downQuestion(target);
			});
			$("<a class=\"qtiBox-tool-collapse\" href=\"javascript:void(0)\"></a>").appendTo(operTool).bind("mousedown", function() {
				if (opts.collapsed == true) {
					expandPanel(target, true);
				} else {
					collapseBox(target, true);
				}
			});
			$("<a class=\"qtiBox-tool-del\" href=\"javascript:void(0)\"></a>").appendTo(operTool).bind("mousedown", function() {
				destroyBox(target);
				$(opts.testBox).testBox("setCurrentQuestion",null);
				//删除
				$(opts.testBox).testBox("removeQuestion",opts.question);
				//试卷中减去此题的分数
				var testBoxOpts=$(opts.testBox).testBox("options");
				if(testBoxOpts){
					testBoxOpts.onReduceFullMarks.call(target,opts.question);
				}
				return false;
			});
			pp.children("div.qtiBox-body").removeClass("qtiBox-body-noheader");
		} else {
			pp.children("div.qtiBox-body").addClass("qtiBox-body-noheader");
		}
	};
	
	function setStatusEdit(target){
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		opts.status="edit-question";
		$(target).find(".edit-answer").removeClass("edit-answer").addClass("edit-question");
		$(target).find(".edit-sleep").removeClass("edit-sleep").addClass("edit-question");
		$(target).find("div>.nicEdit-context").attr("contenteditable",true);
		$(target).removeClass("edit-answer").addClass("edit-question");
		$(target).removeClass("edit-sleep").addClass("edit-question");
		if(opts.responseBut.hasClass("selected")){
			opts.responseBut.removeClass("selected");
			opts.responseBut.addClass("link");
		}
		
		if(opts.questionBut.hasClass("link")){
			opts.questionBut.removeClass("link");
			opts.questionBut.addClass("selected");
		}
	}
	function setStatusAnswer(target){
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		opts.status="edit-answer";
		$(target).find("div>.nicEdit-context").removeAttr("contenteditable");
    	$(target).find(".edit-question").removeClass("edit-question").addClass("edit-answer");
    	$(target).find(".edit-sleep").removeClass("edit-sleep").addClass("edit-answer");
    	
    	$(target).removeClass("edit-question").addClass("edit-answer");
    	$(target).removeClass("edit-sleep").addClass("edit-answer");
    	if(opts.questionBut.hasClass("selected")){
			opts.questionBut.removeClass("selected");
			opts.questionBut.addClass("link");
		}
		
		if(opts.responseBut.hasClass("link")){
			opts.responseBut.removeClass("link");
			opts.responseBut.addClass("selected");
		}
	}
	
	function setSleep(target){
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		//是否为收缩，状态
		if(!opts.collapsed){
			var pp = qtiBox.qtiBox;
			removeNode(pp.children("div.qtiBox-header"));
		}
		opts.status="edit-sleep";
    	$(target).find(".edit-question").removeClass("edit-question").addClass("edit-sleep");
    	$(target).find(".edit-answer").removeClass("edit-answer").addClass("edit-sleep");
    	
    	$(target).find("div>.nicEdit-context").removeAttr("contenteditable");
    	$(target).removeClass("edit-question").addClass("edit-sleep");
    	$(target).removeClass("edit-answer").addClass("edit-sleep");
    	opts.clearAnswerCall(target);
	};
	
	function setEdit(target){
		var qtiBox = $.data(target, "qtiBox");
		var opts = qtiBox.options;
		//是否为收缩，状态
		if(!opts.collapsed && opts.status=="edit-sleep"){
			opts.status="edit-question";
			setHeader(target);
		}
		setStatusEdit(target);
	};
	
	function clear(target) {
		var t = $(target);
		t.find(".combo-f").each(function() {
			$(this).combo("destroy");
		});
		t.find(".m-btn").each(function() {
			$(this).menubutton("destroy");
		});
		t.find(".s-btn").each(function() {
			$(this).splitbutton("destroy");
		});
		t.find(".tooltip-f").each(function() {
			$(this).tooltip("destroy");
		});
		t.children("div").each(function() {
			$(this)._size("unfit");
		});
		t.empty();
	};
	function setResizable(target) {
		$(target).qtiBox("doLayout", true);
	};
	function openPanel(target, forceOpen) {
		var opts = $.data(target, "qtiBox").options;
		var qtiBox = $.data(target, "qtiBox").qtiBox;
		if (forceOpen != true) {
			if (opts.onBeforeOpen.call(target) == false) {
				return;
			}
		}
		qtiBox.show();
		opts.closed = false;
		var restore = qtiBox.children("div.qtiBox-header").find("a.qtiBox-tool-restore");
		if (restore.length) {
			opts.maximized = true;
		}
		opts.onOpen.call(target);
		if (opts.collapsed == true) {
			opts.collapsed = false;
			collapseBox(target);
		}
		if (!opts.collapsed) {
			setResizable(target);
		}
	};
	function destroyBox(target, forceDestroy) {
		var opts = $.data(target, "qtiBox").options;
		var qtiBox = $.data(target, "qtiBox").qtiBox;
		if (forceDestroy != true) {
			if (opts.onBeforeDestroy.call(target) == false) {
				return;
			}
		}
		$(target).qtiBox("clear");
		removeNode(qtiBox);
		opts.onDestroy.call(target);
	};
	function collapseBox(target, animate) {
		var opts = $.data(target, "qtiBox").options;
		var qtiBox = $.data(target, "qtiBox").qtiBox;
		var qtiBoxBody = qtiBox.children("div.qtiBox-body");
		var qtiBoxfooter = qtiBox.children("div.qtiBox-footer");
		var qtiBoxHeader = qtiBox.children("div.qtiBox-header").find("a.qtiBox-tool-collapse");
		if (opts.collapsed == true) {
			return;
		}
		qtiBoxBody.stop(true, true);
		if (opts.onBeforeCollapse.call(target) == false) {
			return;
		}
		qtiBoxHeader.addClass("qtiBox-tool-expand");
		if (animate == true) {
			qtiBoxBody.slideUp("normal", function() {
				opts.collapsed = true;
				opts.onCollapse.call(target);
			});
		} else {
			qtiBoxBody.hide();
			qtiBoxfooter.hide();
			opts.collapsed = true;
			opts.onCollapse.call(target);
		}
	};
	function expandPanel(target, animate) {
		var opts = $.data(target, "qtiBox").options;
		var qtiBox = $.data(target, "qtiBox").qtiBox;
		var qtiBoxBody = qtiBox.children("div.qtiBox-body");
		var qtiBoxfooter = qtiBox.children("div.qtiBox-footer");
		var qtiBoxHeader = qtiBox.children("div.qtiBox-header").find("a.qtiBox-tool-collapse");
		if (opts.collapsed == false) {
			return;
		}
		qtiBoxBody.stop(true, true);
		if (opts.onBeforeExpand.call(target) == false) {
			return;
		}
		qtiBoxHeader.removeClass("qtiBox-tool-expand");
		if (animate == true) {
			qtiBoxBody.slideDown("normal", function() {
				opts.collapsed = false;
				opts.onExpand.call(target);
				setResizable(target);
			});
		} else {
			qtiBoxBody.show();
			qtiBoxfooter.show();
			opts.collapsed = false;
			opts.onExpand.call(target);
			setResizable(target);
		}
	};
	function setTitle(target, title) {
		$.data(target, "qtiBox").options.title = title;
		$(target).qtiBox("header").find("div.qtiBox-title").html(title);
	};
	var resized = null;
	$(window).unbind(".qtiBox").bind("resize.qtiBox", function() {
		if (resized) {
			clearTimeout(resized);
		}
		resized = setTimeout(function() {
			var layout = $("body.layout");
			if (layout.length) {
				layout.layout("resize");
			} else {
				$("body").qtiBox("doLayout");
			}
			resized = null;
		}, 100);
	});
	$.fn.qtiBox = function(method, options) {
		if (typeof method == "string") {
			return $.fn.qtiBox.methods[method](this, options);
		}
		method = method || {};
		return this.each(function() {
			var qtiBox = $.data(this, "qtiBox");
			var opts;
			if (qtiBox) {
				opts = $.extend(qtiBox.options, method);
				qtiBox.isLoaded = false;
			} else {
				opts = $.extend({}, $.fn.qtiBox.defaults, $.fn.qtiBox.parseOptions(this), method);
				$(this).attr("title", "");
				qtiBox = $.data(this, "qtiBox", {
					options : opts,
					qtiBox : wrapQtiBox(this),
					isLoaded : false
				});
			}
			setBorder(this);
			if (opts.doSize == true) {
				qtiBox.qtiBox.css("display", "block");
				resizeBox(this);
			}
			if (opts.closed == true) {
				qtiBox.qtiBox.hide();
			} else {
				openPanel(this);
			}
		});
	};
	$.fn.qtiBox.methods = {
		options : function(jq) {
			return $.data(jq[0], "qtiBox").options;
		},
		qtiBox : function(jq) {
			return $.data(jq[0], "qtiBox").qtiBox;
		},
		getIframe : function(jq) {
			return $.data(jq[0], "qtiBox").options.iframeObject;
		},
		header : function(jq) {
			return $.data(jq[0], "qtiBox").qtiBox.find(">div.qtiBox-header");
		},
		body : function(jq) {
			return $.data(jq[0], "qtiBox").qtiBox.find(">div.qtiBox-body");
		},
		setTitle : function(jq, title) {
			return jq.each(function() {
				setTitle(this, title);
			});
		},
		setSleep:function(jq){
			return jq.each(function() {
				setSleep(this);
			});
		},
		setEdit:function(jq){
			return jq.each(function() {
				setEdit(this);
			});
		},
		setStatusAnswer:function(jq){
			return jq.each(function() {
				setStatusAnswer(this);
			});
		},
		setStatusEdit:function(jq){
			return jq.each(function() {
				setStatusEdit(this);
			});
		},
		open : function(jq, forceOpen) {
			return jq.each(function() {
				openPanel(this, forceOpen);
			});
		},
		destroy : function(jq, forceDestroy) {
			return jq.each(function() {
				destroyBox(this, forceDestroy);
			});
		},
		clear : function(jq) {
			return jq.each(function() {
				clear(this);
			});
		},
		resize : function(jq, options) {
			return jq.each(function() {
				resizeBox(this, options);
			});
		},
		doLayout : function(jq, all) {
			return jq.each(function() {
						var _6a = this;
						var body = _6a == $("body")[0];
						var s = $(this).find("div.qtiBox:visible,div.accordion:visible,div.tabs-container:visible,div.layout:visible,.easyui-fluid:visible").filter(function(_6c, el) {
							var p = $(el).parents("div.qtiBox-body:first");
							if (body) {
								return p.length == 0;
							} else {
								return p[0] == _6a;
							}
						});
						s.trigger("_resize", [ all || false ]);
					});
		},
		collapse : function(jq, animate) {
			return jq.each(function() {
				collapseBox(this, animate);
			});
		},
		expand : function(jq, animate) {
			return jq.each(function() {
				expandPanel(this, animate);
			});
		},
		calculationAnswer : function(jq, animate) {
			return jq.each(function() {
				var opts=$.data(this, "qtiBox").options;
				opts.questionButEvent(this);
			});
		}
	};
	$.fn.qtiBox.parseOptions = function(target) {
		return $.extend({}, $.parser.parseOptions(target, [ "id", "width",
			"height", "title", "cls",
			"headerCls", "bodyCls",  "method", {
				cache : "boolean",
				border : "boolean",
				noheader : "boolean"
			}, {
				collapsible : "boolean",
			}, {
				closable : "boolean",
				collapsed : "boolean",
				closed : "boolean"
			} ]));
	};
	$.fn.qtiBox.defaults = {
		id : null,
		title : null,
		width : "100%",
		height : "auto",
		question:null,
		scoreInput:null,
		cls : null,
		headerCls : null,
		bodyCls : null,
		questionBut:null,
		responseBut:null,
//		status:"edit-question",
		status:"edit-sleep",
		questionButEvent:function(target){},
		responseButEvent:function(target){},
		//清楚答案
		clearAnswerCall:function(target){},
		//设置答案
		setAnswerCall:function(target){},
		testBox:null,
		style : {},
		cache : true,
		border : true,
		doSize : true,
		noheader : false,
		collapsible : false,
		collapsed : false,
		closed : false,
		extractor : function(data) {
			var pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
			var matches = pattern.exec(data);
			if (matches) {
				return matches[1];
			} else {
				return data;
			}
		},
		contentCall:function(target){},
		onBeforeOpen : function() {},
		onOpen : function() {},
		onBeforeDestroy : function() {},
		onDestroy : function() {},
		onResize : function(_6c, _6d) {},
		onBeforeCollapse : function() {},
		onBeforeExpand : function() {},
		onCollapse : function() {},
		onExpand : function() {},
	};
})(jQuery);
